package com.example.tnot.Teacher.MinePart.InternalFragment.AboutStu;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tnot.R;
import com.example.tnot.Teacher.BaseFragment.BaseBackFragment;
import com.example.tnot.Teacher.MinePart.InternalFragment.AboutStu.adapter.StudentAdapter;
import com.example.tnot.Teacher.MinePart.InternalFragment.AboutStu.itembean.Student;
import com.example.tnot.Util.ToastUtil;

public class StuInforEditFragment extends BaseBackFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    View view;
    ImageButton ib_back;
    EditText et_name, et_id, et_phone,et_Class;
    TextView tv_PreViewName, tv_PreViewId, tv_PreViewPhone,tv_PreViewClass;
    Button btn_save,btn_confirm;

    StuInforEditFragmentArgs args;
    private String mParam1;
    private String mParam2;

    public StuInforEditFragment() {
    }
    public static StuInforEditFragment newInstance(String param1, String param2) {
        StuInforEditFragment fragment = new StuInforEditFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.mine_stu_infor_edit_fragment, container, false);
        //初始化控件
        InitView();
        //初始化界面
        InitLayoutView();
        //将被修改学生的信息默认填入修改框内
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkInputFields()){
                    ContainInfor();
                }

            }
        });

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                String ClassName = tv_PreViewClass.getText().toString();
//                String name = tv_PreViewName.getText().toString();
//                String id = tv_PreViewId.getText().toString();
//                String phone = tv_PreViewPhone.getText().toString();
//                Student student = new Student(name,id,phone,ClassName);
                //更新
                NavController navController = Navigation.findNavController(v);
                navController.navigateUp();
            }
        });
        ib_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(v);
                navController.navigateUp();
            }
        });

        return view;
    }

    private void InitView() {
        args = StuInforEditFragmentArgs.fromBundle(getArguments());
        ib_back = view.findViewById(R.id.ib_edit_back);
        et_name = view.findViewById(R.id.et_name);
        et_id = view.findViewById(R.id.et_StudentId);
        et_phone = view.findViewById(R.id.et_Phone);
        et_Class= view.findViewById(R.id.et_Class);
        tv_PreViewPhone = view.findViewById(R.id.tv_PreviewPhone);
        tv_PreViewId = view.findViewById(R.id.tv_PreviewStudentId);
        tv_PreViewName = view.findViewById(R.id.tv_PreviewName);
        tv_PreViewClass = view.findViewById(R.id.tv_PreviewClass);
        btn_confirm = view.findViewById(R.id.btn_confirm);
        btn_save = view.findViewById(R.id.btn_save);
    }
    private void InitLayoutView() {
        if(args != null){
            String name = args.getName();
            String id = args.getId();
            String phone = args.getPhone();
            String className = args.getClassName();
            et_name.setText(name);
            et_id.setText(id);
            et_phone.setText(phone);
            et_Class.setText(className);
        }
    }
    private void ContainInfor() {
        tv_PreViewName.setText(et_name.getText().toString());
        tv_PreViewId.setText(et_id.getText().toString());
        tv_PreViewPhone.setText(et_phone.getText().toString());
        tv_PreViewClass.setText(args.getClassName());
    }
    private boolean checkInputFields() {
        String name = et_name.getText().toString().trim();
        String id = et_id.getText().toString().trim();
        String phone = et_phone.getText().toString().trim();
        String className = et_Class.getText().toString().trim();

        if (name.isEmpty()) {
            showToast("请输入学生姓名");
            et_name.requestFocus();
            return false;
        }

        if (id.isEmpty()) {
            showToast("请输入学生学号");
            et_id.requestFocus();
            return false;
        }

        if (phone.isEmpty()) {
            showToast("请输入学生电话");
            et_phone.requestFocus();
            return false;
        }

        if (className.isEmpty()) {
            showToast("请输入学生班级");
            et_Class.requestFocus();
            return false;
        }

        // 手机号格式验证
        if (!isValidPhone(phone)) {
            showToast("请输入正确的手机号码");
            et_phone.requestFocus();
            return false;
        }

        return true;
    }

    private boolean isValidPhone(String phone) {
        // 简单的手机号验证（11位数字）
        return phone.matches("^1[3-9]\\d{9}$");
    }

    private void showToast(String message) {
        if (requireActivity() != null) {
            ToastUtil.showCustomToast(requireActivity(), message);
        }
    }

}